import {Body, Controller, Get, HttpStatus, Post, Query, Res, Headers} from '@nestjs/common';
import {Response} from 'express';
import {CommonResponseDto} from '../dto/common.response.dto';
import {ResCode, UserStatus} from '../common/constants';
import {OperateLogService} from '../service/operate.log.service';
import {PaginationDataDto} from "../dto/pagination.data.dto";


@Controller('api/log')
export class OperateLogController {
    constructor(private readonly operateLogService: OperateLogService) {}

    @Post('/list')
    async getOperateLogList(@Body() params: {pageIndex: number, pageSize: number, userId: string, type: string}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        params.pageIndex = params.pageIndex || 1
        params.pageSize = params.pageSize || 10
        params.userId = params.userId || ''
        params.type = params.type || ''
        try {
            const data = await this.operateLogService.getListByPaginationAndCondition(params.pageIndex, params.pageSize, params.userId, params.type)
            if (data && data.length === 2) {
                const pageTotal = Math.ceil(data[1]/params.pageSize);
                return new CommonResponseDto(ResCode.OK, 'success', new PaginationDataDto(
                    params.pageIndex, params.pageSize, pageTotal, data[1], data[0]
                ))
            }
            return new CommonResponseDto(ResCode.ERROR, '获取数据失败')
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '获取数据异常')
        }
    }
}
